package cn.ruituo.web;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet",value = {"/LoginServlet","/login","/denglu"},initParams =@WebInitParam(name = "encoding",value = "gbk") )

public class LoginServlet extends HttpServlet {

    private String zif="utf-8";
    @Override
    public void init(ServletConfig config) throws ServletException {
        zif=config.getInitParameter("encoding");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Part part = request.getPart("");
//        part.write();
        //super.getServletContext()
        //ServletContext application = request.getServletContext();
        //super.getServletConfig().getInitParameter("")
        //request.setCharacterEncoding("utf-8");
        System.out.println(zif+"??????????????????");
        request.setCharacterEncoding(zif);
        String u=request.getParameter("uname");
        String p=request.getParameter("upass");
        if(u.startsWith("admin")&&p.equals("123456")){
            HttpSession session = request.getSession();
            session.setAttribute("USER_LOGIN",u);
            response.sendRedirect("welcome.jsp");
            return;
        }
        response.setContentType("text/html;charset=utf-8");

        PrintWriter writer = response.getWriter();
        writer.print("<script>alert('错误的用户名或者密码');location='login2.jsp';</script>");
        //request.setAttribute("msg","错误的用户名或者密码");
       // request.getRequestDispatcher("/login2.jsp").forward(request,response);

    }
}
